package com.msgqu.share.controller;

import com.google.common.collect.Maps;
import com.msgqu.share.domain.common.MinioUploadResult;
import com.msgqu.share.domain.common.Result;
import com.msgqu.share.domain.user.TUser;
import com.msgqu.share.service.impl.UserServiceComsumer;
import com.msgqu.share.utils.MinioUtil;
import com.msgqu.share.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.net.URL;
import java.util.Map;

/**
 * @description:
 * @author: wsj
 * @create: 2020-11-03 12:27
 * @throws:
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserServiceComsumer userServiceComsumer;

    @GetMapping("/message")
    public String getMessage(){
        return userServiceComsumer.getMessage();
    }
    @GetMapping("/user")
    public TUser getUserById(Long nId){
        return userServiceComsumer.getUserById(nId);
    }

    @GetMapping("/putredis")
    public String put(@RequestParam("key") String key,
                      @RequestParam("value") String value){
//        redisTemplate.opsForValue().set(key,value);
//        RedisUtil.setString(key,value);
        Map<String,Object> map1 = Maps.newHashMap();
        Map<String,Object> map2 = Maps.newHashMap();
        map2.put("name","zhangsan");
        map2.put("age",21);
        map1.put(key,map2);
        RedisUtil.hashPutAll(key,map1);
        return "OK";
    }
    @GetMapping("/get/{key}")
    public Object get(@PathVariable String key){
//        String value = (String) redisTemplate.opsForValue().get(key);
//        String value = RedisUtil.getString(key);
        Map<Object, Object> map = RedisUtil.hashEntries(key);
        return map;
    }

    int i = 100;

    @GetMapping("/lock")
    public String lock(){
        RLock rLock = RedisUtil.getLock("aa");
        try {
            rLock.lock();
            i = i-1;
            log.info("i={}",i);
        }finally {
            RedisUtil.unLock(rLock);
        }
        return "ok";
    }
    @PostMapping("/fileupload")
    public Result fileUpload(@RequestParam("file") MultipartFile file){
        MinioUploadResult minioUploadResult = MinioUtil.putObject(file,"test-test1");
        return Result.success(minioUploadResult);
    }
    @GetMapping("/download")
    public void download(HttpServletResponse response) throws Exception {
//        MinioUtil.download(response,"test-test1","2020/11/26/789F6A15465040E08363728C430C2FB0.jpg","照骗.jpg");
        String url = "http://localhost:8653/sufu/kyla/20201123/%E6%96%B0%E8%AF%81%E6%8D%AE%E6%9D%90%E6%96%99.pdf?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=splcgk%2F20201126%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20201126T092020Z&X-Amz-Expires=604800&X-Amz-SignedHeaders=host&X-Amz-Signature=8883b297f70a3802fe6ee3f1bbbe34a6ea6c99b4235c6ab2ac5e0d7e6097dd9e";
        URL httpurl = new URL(url);
        FileUtils.copyURLToFile(httpurl,new File("/Users/wushuangjian/Desktop/img/file.jpg"));
    }

    @GetMapping("/url")
    public Result url(){
        String url = MinioUtil.getPresignedObjectUrl("test-test1", "2020/11/26/789F6A15465040E08363728C430C2FB0.jpg");
        return Result.success(url);
    }
}
